package com.jzqf.smart.controller;

import com.jzqf.smart.entity.BaseResponse;
import com.jzqf.smart.entity.User;
import com.jzqf.smart.service.UserService;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @CrossOrigin
    @RequestMapping(value = "/user/getUser", method = RequestMethod.GET)
    public BaseResponse<List<User>> getUser() {
        return new BaseResponse<List<User>>(200, null, userService.getAllUser());
    }

    @CrossOrigin
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResponse login(@RequestBody User user) {
        try {
            return new BaseResponse(200, "添加成功", userService.login(user));
        } catch (Exception e) {
            return new BaseResponse(500, "登录失败" + e.getMessage());
        }
    }

    @CrossOrigin
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public BaseResponse register(@RequestBody User user) {
        try {
            if (userService.register(user) > 0) {
                return new BaseResponse(200, "添加成功");
            } else {
                return new BaseResponse(200, "添加用户失败");
            }
        } catch (Exception e) {
            return new BaseResponse(500, e.getMessage());
        }
    }

    /**
     * 文件上传
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/fileupload")
    public String fileupload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到上传文件的保存目录，将上传的文件存放于WEB-INF目录下，不允许外界直接访问，保证上传文件的安全
        String savePath = "D:/Develop";
        File file = new File(savePath);
        //判断上传文件的保存目录是否存在
        if (!file.exists() && !file.isDirectory()) {
            System.out.println(savePath + "目录不存在，需要创建");
            //创建目录
            file.mkdir();
        }
        //消息提示
        String message = "";
        try {
            //使用Apache文件上传组件处理文件上传步骤：
            //1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、创建一个文件上传解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
            //解决上传文件名的中文乱码
            upload.setHeaderEncoding("UTF-8");
            //3、判断提交上来的数据是否是上传表单的数据
            if (!ServletFileUpload.isMultipartContent(request)) {
                //按照传统方式获取数据
                System.out.println("没有文件上传");
                return "index.html";
            }
            //4、使用ServletFileUpload解析器解析上传数据，解析结果返回的是一个List<FileItem>集合，
            // 每一个FileItem对应一个Form表单的输入项
            Map<String, List<FileItem>> stringListMap = upload.parseParameterMap(request);
            List<FileItem> list = (List<FileItem>) stringListMap;
            for (FileItem item : list) {
                //如果fileitem中封装的是普通输入项的数据
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    //解决普通输入项的数据的中文乱码问题
                    String value = item.getString("UTF-8");
                    //value = new String(value.getBytes("iso8859-1"),"UTF-8");
                    System.out.println(name + "=" + value);
                } else {//如果fileitem中封装的是上传文件
                    //得到上传的文件名称，
                    String filename = item.getName();
                    System.out.println(filename);
                    if (filename == null || filename.trim().equals("")) {
                        continue;
                    }
                    //注意：不同的浏览器提交的文件名是不一样的，有些浏览器提交上来的文件名是带有路径的，如：  c:\a\b\1.txt，而有些只是单纯的文件名，如：1.txt
                    //处理获取到的上传文件的文件名的路径部分，只保留文件名部分
                    filename = filename.substring(filename.lastIndexOf("\\") + 1);
                    //获取item中的上传文件的输入流
                    InputStream in = item.getInputStream();
                    //创建一个文件输出流
                    FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
                    //创建一个缓冲区
                    byte buffer[] = new byte[1024];
                    //判断输入流中的数据是否已经读完的标识
                    int len = 0;
                    //循环将输入流读入到缓冲区当中，(len=in.read(buffer))>0就表示in里面还有数据
                    while ((len = in.read(buffer)) > 0) {
                        //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                        out.write(buffer, 0, len);
                    }
                    //关闭输入流
                    in.close();
                    //关闭输出流
                    out.close();
                    //删除处理文件上传时生成的临时文件
                    item.delete();
                    message = "文件上传成功！";
                }
            }
        } catch (Exception e) {
            message = "文件上传失败！";
            e.printStackTrace();

        }
//       request.setAttribute("message",message);
//       request.getRequestDispatcher("/message.jsp").forward(request, response);
        return "index.html";
    }

    //上传文件会自动绑定到MultipartFile中
    @RequestMapping(value = "/upload", method = {RequestMethod.POST})
    public BaseResponse upload(HttpServletRequest request,
                               @RequestParam("des") String des,
                               @RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("开始接收了");
        BaseResponse result = new BaseResponse();
        //如果文件不为空，写入上传路径
        if (!file.isEmpty()) {
            //上传文件路径
            String path = "D:/Develop";
            //上传文件名
            String filename = file.getOriginalFilename();
            System.out.println("path=" + path + ",desc=" + des);
            System.out.println("filename=" + filename);
            File filepath = new File(path, filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            file.transferTo(filepath);
            result.setStatus(200);
            result.setData(filename);
            return result;
        } else {
            return result;
        }

    }

    //上传文件会自动绑定到MultipartFile中
    @RequestMapping(value = "/uploads", method = {RequestMethod.POST})
    public BaseResponse uploads(HttpServletRequest request,
                                @RequestParam("des") String des,
                                @RequestParam("files") List<MultipartFile> files) throws Exception {
        System.out.println("开始接收了");
        BaseResponse result = new BaseResponse();
        if (files.isEmpty()) {
            result.setStatus(500);
            result.setMessage("files is null");
            return result;
        }
        //上传文件路径
        String path = "D:/Develop";
        files.stream().forEach(multipartFile -> {
            File filepath = new File(path, Objects.requireNonNull(multipartFile.getOriginalFilename()));
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            try {
                multipartFile.transferTo(filepath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        result.setStatus(200);
        result.setData("上传成功");
        return result;
    }

}
